package com.zys.controller;

import com.zys.pojo.Manager;
import com.zys.entity.Result;
import com.zys.service.ManagerService;
import com.zys.constant.MessageConstant;

import org.springframework.util.DigestUtils;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;


/**
 * 处理登录
 */
@RestController
public class LoginController {

  @Reference
  private ManagerService managerService;

  /**
   * 登录验证
   *
   * @param manager 管理名称和密码
   * @return Result
   */
  @RequestMapping("/login")
  public Result login(@RequestBody Manager manager) {
    try {
      manager.setPassword(DigestUtils.md5DigestAsHex(manager.getPassword().getBytes()));
      return new Result(true, MessageConstant.LOGIN_SUCCESS,
          managerService.findManagerBymNameAndPassword(manager));
    } catch (Exception e) {
      e.printStackTrace();
      return new Result(false, MessageConstant.LOGIN_FAIL);
    }
  }
}
